package a10_动态规划;

/**
 * <p>
 * a17_一和零复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/3/26
 */
public class a17_一和零复习3 {
    public static void main(String[] args) {
        findMaxForm(new String[]{"10","0001","111001","1","0"}, 5, 3);
    }
    //每个物体的重量：x,y
    //最大容量：m,n

    //0,1背包问题 逆时针
    //dp[i][j]:容量为i个0j个1的背包装的最多的物品
    public static int findMaxForm(String[] strs, int m, int n) {
        int[][] dp = new int[m+1][n+1];

        for (String str : strs) {
            char[] chars = str.toCharArray();
            int x = 0;
            int y = 0;
            for (char c : chars) {
                if(c == '0'){
                    x++;
                }else {
                    y++;
                }
            }

            for (int i = m; i >= 0; i--) {
                for (int j = n; j >= 0; j--) {
                    if(i >= x && j >= y){
                        dp[i][j] = Math.max(dp[i][j], dp[i-x][j-y] + 1);
                    }
                }
            }
        }
        return dp[m][n];
    }
}
